<?php

class AclHook {

  var $CI;

  function AclHook() {
    $this->CI = &get_instance();
  }

  function check_permission() {
    return true;
    if (str_startsWith(uri_string(), 'pages/'))
      return true;
    $controller = $this->CI->router->fetch_class();
    $action = $this->CI->uri->rsegment(2);
    $action = $action == 'index' ? '' : $action;
    if ($this->CI->permission_model->count(array('controller' => $controller, 'action' => $action)) <= 0) {
      return true;
    }
    if (userdata('id')) {
      $user = $this->CI->user_model->get(userdata('id'));
      if ($user) {
        $group = $this->CI->group_model->get($user['group_id']);
        if ($group && $group['name'] == 'admin')
          return true;
        $permission = $this->CI->permission_model->get(array('grant_id' => $user['id'],
                    'type' => 'user','controller' => $controller,'action' => $action,'accept' => 1));
        if (!$permission) {
          $permission = $this->CI->permission_model->get(array('grant_id' => $user['group_id'],
                      'type' => 'group','controller' => $controller,'action' => $action,'accept' => 1));
          if ($permission) {
            return true;
          }
        } else {
          return true;
        }
      }
    }
    return false;
  }

  public function check_acl() {
    if (!$this->check_permission()) {
      set_flash_error($this->CI->lang->line('error.access.denied'));
//      redirect('admin');
      redirect('login/access_denied');
      return;
    }
  }

}
